<template>
  <div class="layout layout-100">
    <Layout class="layout-100">
      <Header class="header-cont main__title--top">标准WBS{{this.$route.params.name}}</Header>
      <Content class="content-cont">
        <Form  ref="formData" :model="formData" label-position="top"  :show-message="false">
          <FormItem label="编号" prop="code"  >
            <Input class="width50" v-model.trim="formData.code" :disabled="true"></Input>
          </FormItem>
          <FormItem label="名称" prop="name" :required="true">
            <Input class="width50" v-model.trim="formData.name" maxlength='60' :disabled="$route.params.type === 'details'"></Input>
          </FormItem>
        </Form>
      </Content>
      <Footer class="footer-cont" v-show="$route.params.type !== 'details'">
        <Button type="primary" @click="saveSubmit('formData')">保存</Button>
        <Button @click="closetab">取消</Button>
      </Footer>
    </Layout>
  </div>
</template>

<script>
  import { mapMutations } from 'vuex'
  export default {
    name: "AddEditor",
    data(){
      return{
        typeList:[],
        formData: {
          name: '',
          code:'',
        },
      }
    },
    methods: {
      ...mapMutations([
        'closeTag'
      ]),
      closetab () {
        this.closeTag({
          name: 'add-editor',
          params: {
            id: this.$route.params.id,
            name:this.$route.params.name,
            type: this.$route.params.type,
          },
          query:{
            current: this.$route.query.current,
            pageSize:this.$route.query.pageSize,
            parentId:this.$route.query.parentId,
            dictEngineeringType:this.$route.query.dictEngineeringType,
            code:this.$route.query.code
          }
        });
      },

      saveSubmit (name) {
        if(!this.formData.name){
          this.$Message.warning('名称不能为空');
          return;
        }
        let params = {
          "name": this.formData.name,
          "code": this.formData.code,
          "parentId":this.$route.query.parentId,
          "dictEngineeringType":this.$route.query.dictEngineeringType,
        };
        if(this.$route.params.type === 'editor'){
          params.id = this.$route.params.id;
          this.$api.standardLibrary.getProfessionalTypeEditor(params).then((res) => {
            if(res.code === 200){
              this.$Message.success(res.message);
              this.closetab();
              this.$router.push({
                path:'/standard-wbs'
              })
              localStorage.setItem('page',JSON.stringify(this.page))
            }else {
              this.$Message.error(res.message);
            }
          })
        }else {
          this.$api.standardLibrary.getProfessionalTypeAdd(params).then((res) => {
            if(res.code === 200){
              this.$Message.success(res.message);
              this.closetab();
              this.$router.push({
                path:'/standard-wbs'
              });
              localStorage.setItem('page',JSON.stringify(this.page))
            }else {
              this.$Message.error(res.message);
            }
          })
        }
      },
      //详情
      getDetail(id){
        if(this.$route.params.name === '日志详情'){
          this.$api.operationLog.getOperationLogDetail(id).then((res) => {
            if(res.code === 200){
              let data = JSON.parse(res.data.params);
              this.formData = {...data}
            }else {
              this.$Message.error(res.message);
            }
          })
        }else {
          this.$api.standardLibrary.getProfessionalTypedetail(id).then((res) => {
            if(res.code === 200){
              this.formData = {...res.data}
            }else {
              this.$Message.error(res.message);
            }
          })
        }

      },
      initParams() {
        this.page = this.$route.query;
        this.formData.code =this.page.code;
      }
    },
    mounted () {
      this.initParams();
      if(this.$route.params.type === 'editor' || this.$route.params.type === 'details'){
        this.getDetail(this.$route.params.id)
      }
    }
  }
</script>

<style lang="scss" scoped>
  .width50{
    width: 40%;
  }
</style>
